package com.sun.hui_location;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;

public class StreamEventLocaton implements EventChannel.StreamHandler {

    //event管道
    private EventChannel mEventChannel;
    private Activity mActivity;
    private BinaryMessenger binaryMessenger;
    //event数据流
    private EventChannel.EventSink mEvents;
    private LocationManager locationManager;
    private  String PROVIDER ;

    StreamEventLocaton(Activity mActivity, BinaryMessenger binaryMessenger, LocationManager locationManager, String provider){
        this.mActivity = mActivity;
        this.binaryMessenger = binaryMessenger;
       // this.PROVIDER = provider;
        this.locationManager = locationManager;

        mEventChannel = new EventChannel(binaryMessenger,"sqh.rr/location");
        mEventChannel.setStreamHandler(this);
    }

    @Override
    public void onListen(Object arguments, EventChannel.EventSink events) {
        mEvents = events;
        Log.e("传过来的值", "onListen: "+arguments.toString());
        if (arguments.toString().equals("[gps]")){
            PROVIDER = LocationManager.GPS_PROVIDER;
        }else {
            PROVIDER = LocationManager.NETWORK_PROVIDER;
        }

         LocationPermission permission = new LocationPermission(mActivity);
        if (!permission.checkPermissions(Manifest.permission.ACCESS_FINE_LOCATION)) {
            permission.requestPermissions(Manifest.permission.ACCESS_FINE_LOCATION);
            return;
        }

        if (!permission.checkPermissions(Manifest.permission.ACCESS_COARSE_LOCATION)) {
            permission.requestPermissions(Manifest.permission.ACCESS_COARSE_LOCATION);
            return;
        }

        LocationListener listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.e("duix", "onListen: "+PROVIDER);
                if (location != null ) {
                    Map<String, Double> locationValue = generateResult(location);
                    mEvents.success(locationValue);
                    //result.success(location.getLatitude());
                }
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }

            @Override
            public void onProviderEnabled(String provider) {
            }

            @Override
            public void onProviderDisabled(String provider) {
            }
        };

        locationManager.requestLocationUpdates(PROVIDER,
                1000, 5, listener, mActivity.getMainLooper());
    }

    @Override
    public void onCancel(Object arguments) {

    }

    private Map<String, Double> generateResult(Location location) {
        HashMap<String, Double> loc = new HashMap<>();
        Log.e("TAG333", "generateResult: "+location.getLatitude());
        loc.put("latitude", location.getLatitude());
        loc.put("longitude", location.getLongitude());
        loc.put("accuracy", (double) location.getAccuracy());
        loc.put("altitude", location.getAltitude());
        loc.put("speed", (double) location.getSpeed());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            loc.put("speed_accuracy", (double) location.getSpeedAccuracyMetersPerSecond());
        }
        loc.put("heading", (double) location.getBearing());
        return loc;
    }

}
